package task

import (
	"time"
	"tushare/model/aliyun/ecs"
	"tushare/service/aliyunCli"
)

func (t Task) CreateEcs(v ecs.AliyunEcs) string {
	keyPairName := "hadoop_three"
	ae := ecs.AliyunEcs{}
	ae.TaskRemark = "创建阿里云ecs.c6a.xlarge中"
	ecs.AliyunEcsUpdate(v.Id, ae)
	ci := new(aliyunCli.CreateInstance)
	id := ci.CreateInstance(v.RegionId, v.ZoneId, v.InstanceType)
	time.Sleep(time.Second * 30)

	ae.TaskRemark = "阿里云ecs.c6a.xlarge创建成功"
	ae.InstanceName = "hadoop"
	ae.InstanceId = id
	ae.ImageId = "centos_7_8_x64_20G_alibase_20211028.vhd"
	ecs.AliyunEcsUpdate(v.Id, ae)

	ae.TaskRemark = "阿里云ecs.c6a.xlarge创建公网IP"
	ecs.AliyunEcsUpdate(v.Id, ae)
	ai := new(aliyunCli.AllocatePublicIpAddress)
	ai.AllocatePublicIpAddress(id)
	time.Sleep(time.Second * 30)
	ae.TaskRemark = "阿里云ecs.c6a.xlarge创建公网IP创建成功"
	ecs.AliyunEcsUpdate(v.Id, ae)

	ae.TaskRemark = "阿里云ecs.c6a.xlarge绑定登录秘钥"
	ecs.AliyunEcsUpdate(v.Id, ae)
	akp := new(aliyunCli.AttachKeyPair)
	akp.AttachKeyPair(id, keyPairName)
	time.Sleep(time.Second * 30)
	ae.TaskRemark = "阿里云ecs.c6a.xlarge登录秘钥绑定成功"
	ecs.AliyunEcsUpdate(v.Id, ae)

	ae.TaskRemark = "阿里云ecs.c6a.xlarge启动"
	ecs.AliyunEcsUpdate(v.Id, ae)
	si := new(aliyunCli.StartInstance)
	si.StartInstance(id)
	time.Sleep(time.Second * 30)
	ae.TaskRemark = "阿里云ecs.c6a.xlarge启动成功"
	ecs.AliyunEcsUpdate(v.Id, ae)

	di := new(aliyunCli.DescribeInstances)
	ip := di.DescribeInstances(id)
	ae.TaskRemark = "阿里云ecs创建完毕"
	ae.Ip = ip
	ae.Status = 1
	ecs.AliyunEcsUpdate(v.Id, ae)

	return id
}
